package controller

import (
	"encoding/json"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"gowork/makeAnAppointment/model"
	"strconv"
)

type MakeAppointmentController struct {
	Ctx iris.Context
	AvailableAppointment *model.AvailableAppointment
}

func (mac *MakeAppointmentController)PostSetupappointments() mvc.Result {

	mac.AvailableAppointment = new(model.AvailableAppointment)

	var dList []string
	dListStr := mac.Ctx.FormValue("dList")
	errj := json.Unmarshal([]byte(dListStr), &dList)
	if errj != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": errj.Error(), "data": nil, "code":500,},
		}
	}

	var tseList []string
	tseListStr := mac.Ctx.FormValue("tseList")
	errt := json.Unmarshal([]byte(tseListStr), &tseList)
	if errt != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": errt.Error(), "data": nil, "code":500,},
		}
	}

	var pList []*model.Professor
	pListStr := mac.Ctx.FormValue("pList")
	errp := json.Unmarshal([]byte(pListStr), &pList)
	if errp != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": errp.Error(), "data": nil, "code":500,},
		}
	}

	snStr := mac.Ctx.FormValue("sn")
	sn, err := strconv.Atoi(snStr)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	meStr := mac.Ctx.FormValue("me")
	me, err := strconv.Atoi(meStr)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := mac.AvailableAppointment.SetUpAppointments(dList, tseList, pList, sn, me)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (mac *MakeAppointmentController)PostQueryappointmentavailable() mvc.Result {

	mac.AvailableAppointment = new(model.AvailableAppointment)

	dIdStr := mac.Ctx.FormValue("dId")
	dId, err := strconv.ParseInt(dIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := mac.AvailableAppointment.QueryAppointmentAvailable(dId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data": res, "code":"200",},
	}
}

func (mac *MakeAppointmentController)PostQueryappointmentavailablenew() mvc.Result {

	mac.AvailableAppointment = new(model.AvailableAppointment)

	dIdStr := mac.Ctx.FormValue("dId")
	dId, err := strconv.ParseInt(dIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := mac.AvailableAppointment.QueryAppointmentAvailableNew(dId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data": res, "code":"200",},
	}
}

func (mac *MakeAppointmentController)PostUserappointment() mvc.Result {

	mac.AvailableAppointment = new(model.AvailableAppointment)

	uIdStr := mac.Ctx.FormValue("uId")
	uId, err := strconv.ParseInt(uIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	uName := mac.Ctx.FormValue("uName")

	aaIdStr := mac.Ctx.FormValue("aaId")
	aaId, err := strconv.ParseInt(aaIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := mac.AvailableAppointment.UserAppointment(uId, uName, aaId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}




















